fmt 태그를 사용하기 위해서 페이지 상단에 다음과 같이 선언되어야 된다.
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt"%>
다국어 문서를 처리할 때 유용하고, 날짜와 숫자 형식을 다루는 fmt 태그는 다음과 같은 종류가 있다.
| 기능 | 태그 | prefix |
|---|---|---|
| Locale 설정 | setLocale, requestEncoding | fmt |
| 메시지 처리 | bundle, message(param), setBundle | fmt |
| 숫자 날짜 형식 | formatNumber, formatDate, parseDate, parseNumber, setTimeZone, timeZone | fmt |
사용자 세션에 대한 로케일 설정
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt"%>
<pre>
default locale : <%= response.getLocale() %>
set locale : ko <fmt:setLocale value="ko" />
now: <%= response.getLocale() %>
set locale : ja <fmt:setLocale value="ja" />
now: <%= response.getLocale() %>
set locale : en <fmt:setLocale value="en" />
now: <%= response.getLocale() %>
</pre>
request.setCharacterEncoding() 역할
<%@ page contentType="text/html;charset=euc-kr" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<fmt:requestEncoding value="euc-kr"/>
파라메터:<c:out value="${param.id}"/>
<form method="post">
<input type="text" name="id">
<input type="submit">
</form>
properties 확장자를 사용하는 자원 파일을 읽어오는 역할
| <fmt:bundle basename="basename" [WEBSTUDY:prefix="prefix"]> body content </fmt:bundle> |
번들 태그에서 정한 값들을 가져옴.
| <fmt:message key="messageKey" [WEBSTUDY:bundle="resourceBundle"] [WEBSTUDY:var="varName"] [scope="{page\ | WEBSTUDY:request|session|application}"]/> |
TestBundle.properties
greeting=Hello.
admin=kenu
TestBundle_ko.properties
greeting=안녕하세요.
admin=허광남
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt"%>
<fmt:setLocale value="ko"/>
<fmt:bundle var="testBundle" basename="com.itexpert.chap9.msg.TestBundle">
<fmt:message>greeting</fmt:message><br>
<fmt:message bundle="${testBundle}" key="admin"/><br>
</fmt:bundle>
페이지 전체에서 사용할 수 있는 번들을 지정.
var 속성에서 정한 변수를 이후에 나오는 <fmt:message/> 태그에서 basename 속성에 변수명으로 대체 가능.
특정 로케일 방식이나, 통화, 퍼센트를 포함하는 숫자 데이타를 디스플레이.
정수와 소수점을 구분하는데 마침표나 콤마를 사용할 것인지 설정.
| <fmt:formatNumber value="numericValue" [type="{number\ | WEBSTUDY:currency|percent}"] [WEBSTUDY:pattern="customPattern"] [WEBSTUDY:currencyCode="currencyCode"] [WEBSTUDY:currencySymbol="currencySymbol"] [groupingUsed="{true\ | WEBSTUDY:false}"] [WEBSTUDY:maxIntegerDigits="maxIntegerDigits"] [WEBSTUDY:minIntegerDigits="minIntegerDigits"] [WEBSTUDY:maxFractionDigits="maxFractionDigits"] [WEBSTUDY:minFractionDigits="minFractionDigits"] [WEBSTUDY:var="varName"] [scope="{page\ | WEBSTUDY:request|session|application}"]/> |
| 속성 | 동적값 | Type | 설명 |
|---|---|---|---|
| value | true | String 또는Number | 형식화될 수치 |
| type | true | String | 숫자, 통화, 퍼센트 중 어느 것으로 표시할 지 지정 {number|currency|percent} |
| pattern | true | String | 사용자가 지정한 형식 패턴. |
| currencyCode | true | String | ISO 4217 통화 코드. 통화 형식일 때만 적용\(type="currency") |
| currencySymbol | true | String | 통화 기호. 통화 형식일 때만 적용\(type="currency") |
| groupingUsed | true | boolean | 형식 출력에 그룹 분리기호를 포함할지 여부 |
| maxIntegerDigits | true | int | 형식 출력에서 integer 최대 자리수 |
| minIntegerDigits | true | int | 형식 출력에서 integer 최소 자리수 |
| maxFractionDigits | true | int | 형식 출력에서 소수점 이하 최대 자리수. |
| minFractionDigits | true | int | 형식 출력에서 소수점 이하 최소 자리수. |
| var | false | String | 형식 출력 결과 문자열을 담는 scope에 해당하는 변수명 |
| scope | false | String | var 의 scope |
value 애트리부트나 바디 콘텐트를 통해 제공된 숫자 데이타를 파싱.
| <fmt:parseNumber value="numericValue" [type="{number\ | WEBSTUDY:currency|percent}"] [WEBSTUDY:pattern="customPattern"] [WEBSTUDY:parseLocale="parseLocale"] [integerOnly="{true\ | WEBSTUDY:false}"] [WEBSTUDY:var="varName"] [scope="{page\ | WEBSTUDY:request|session|application}"]/> |
| 속성 | 동적값 | Type | 설명 |
|---|---|---|---|
| value | true | String 또는Number | 파싱할 수치 |
| type | true | String | 숫자, 통화, 퍼센트 중 어느 것으로 표시할 지 지정 {number|currency|percent} |
| pattern | true | String | 사용자가 지정한 형식 패턴. |
| parseLocale | true | String 또는java.util.Locale | 파싱 작업의 기본 형식 패턴(숫자, 통화, 퍼센트 각각)을 제공하는 Locale |
| integerOnly | true | boolean | 주어진 값에서 integer 부분만 파싱할지 여부를 지정 |
| var | false | String | 파싱 결과 (java.lang.Number 타입)를 담는 scope에 해당하는 변수명 |
| scope | false | String | var 의 scope |
날짜와 시간을 포맷 및 디스플레이 하는데 사용
| <fmt:formatDate value="date" [type="{time\ | WEBSTUDY:date|both}"] [dateStyle="{default\ | WEBSTUDY:short|medium|long|full}"] [timeStyle="{default\ | WEBSTUDY:short|medium|long|full}"] [WEBSTUDY:pattern="customPattern"] [WEBSTUDY:timeZone="timeZone"] [WEBSTUDY:var="varName"] [scope="{page\ | WEBSTUDY:request|session|application}"]/> |
| 속성 | 동적값 | Type | 설명 |
|---|---|---|---|
| value | true | java.util.Date | 형식화될 Date 와 time |
| type | true | String | 형식화할 데이터가 시간, 날짜, 모두 인지 셋 중 하나를 지정한다. |
| dateStyle | true | String | 미리 정의된 날짜 형식. Java.text.DateFormat 클래스에 정의된 문법을 따른다. type="date", type="body", type속성이 생략된 경우 사용. |
| timeStyle | true | String | 미리 정의된 날짜 형식. Java.text.DateFormat 클래스에 정의된 문법을 따른다. type="time", type="body" 의 경우 사용. |
| pattern | true | String | 사용자 지정 형식 스타일 |
| timeZone | true | String 또는java.util.TimeZone | 형식화 시간에 나타날 타임존 |
| var | false | String | 형식 출력 결과 문자열을 담는 scope에 해당하는 변수명 |
| scope | false | String | var 의 scope |
날짜와 시간 값을 파싱하는데 사용
| <fmt:parseDate value="dateString" [type="{time\ | WEBSTUDY:date|both}"] [dateStyle="{default\ | WEBSTUDY:short|medium|long|full}"] [timeStyle="{default\ | WEBSTUDY:short|medium|long|full}"] [WEBSTUDY:pattern="customPattern"] [WEBSTUDY:timeZone="timeZone"] [WEBSTUDY:parseLocale="parseLocale"] [WEBSTUDY:var="varName"] [scope="{page\ | WEBSTUDY:request|session|application}"]/> |
| 속성 | 동적값 | Type | 설명 |
|---|---|---|---|
| value | true | java.util.Date | 파싱할 Date 와 time |
| type | true | String | 파싱할 데이터가 시간, 날짜, 모두 인지 셋 중 하나를 지정한다. |
| dateStyle | true | String | 미리 정의된 날짜 형식. Java.text.DateFormat 클래스에 정의된 문법을 따른다. type="date", type="body", type속성이 생략된 경우 사용. |
| timeStyle | true | String | 미리 정의된 날짜 형식. Java.text.DateFormat 클래스에 정의된 문법을 따른다. type="time", type="body" 의 경우 사용. |
| pattern | true | String | 사용자 지정 형식 스타일 |
| timeZone | true | String 또는java.util.TimeZone | 형식화 시간에 나타날 타임존 |
| parseLocale | true | String 또는java.util.Locale | 파싱하는 동안 적용될 미리 정의된 형식 스타일의 Locale |
| var | false | String | 파싱 결과(java.util.Date)를 담는 scope에 해당하는 변수명 |
| scope | false | String | var 의 scope |
<c:set var="koDateString">06. 4. 1 오후 7:03</c:set>
<fmt:parseDate value="${koDateString}" parseLocale="ko_KR"
type="both" dateStyle="short" timeStyle="short"
var="koDate"/>
<c:set var="usDateString">4/1/06 7:03 PM</c:set>
<fmt:parseDate value="${usDateString}" parseLocale="en_US"
type="both" dateStyle="short" timeStyle="short"
var="usDate"/>
<c:set var="gbDateString">4/1/06 19:03</c:set>
<fmt:parseDate value="${gbDateString}" parseLocale="en_GB"
type="both" dateStyle="short" timeStyle="short"
var="gbDate"/>
<table border="1">
<tr>
<th>Locale</th>
<th>input</th>
<th>output</th>
</tr>
<tr>
<td>Korea Korean(ko_KR)</td>
<td><c:out value="${koDateString}"/></td>
<td><c:out value="${koDate}"/></td>
</tr>
<tr>
<td>U.S. English(en_US)</td>
<td><c:out value="${usDateString}"/></td>
<td><c:out value="${usDate}"/></td>
</tr>
<tr>
<td>British English(en_GB)</td>
<td><c:out value="${gbDateString}"/></td>
<td><c:out value="${gbDate}"/></td>
</tr>
기본 시간대 값을 설정.